package com.wc.算法提高课.A第一章_动态规划.背包模型.能量石;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/10 12:22
 * @description https://www.acwing.com/problem/content/736/
 */
public class Main {
    /**
     * 贪心 + 01背包
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 10010, M = 110;
    static int[] f = new int[N];
    static int[][] a = new int[M][3];
    static int T, n;

    public static void main(String[] args) {
        T = sc.nextInt();
        for (int t = 1; t <= T; t++) {
            n = sc.nextInt();
            int m = 0;
            for (int i = 1; i <= n; i++) {
                for (int j = 0; j < 3; j++) {
                    a[i][j] = sc.nextInt();
                }
                m += a[i][0];
            }
            Arrays.sort(a, 1, n + 1, (o1, o2) -> o1[0] * o2[2] - o2[0] * o1[2]);
            Arrays.fill(f, 0, m + 1, -0x3f3f3f3f);
            f[0] = 0;
            for (int i = 1; i <= n; i++) {
                int s = a[i][0], e = a[i][1], l = a[i][2];
                for (int j = m; j >= s; j--) {
                    f[j] = Math.max(f[j], f[j - s] + e - (j - s) * l);
                }
            }
            int res = 0;
            for(int i = 0; i <= m; i++) res = Math.max(res, f[i]);
            out.printf("Case #%d: %d\n", t, res);
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
